package com.jeesite.modules.utils.ali;


import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.v3.ApiException;
import com.alipay.v3.ApiClient;
import com.alipay.v3.util.model.AlipayConfig;
import com.alipay.v3.Configuration;
import com.alipay.v3.api.AlipayFundTransUniApi;
import com.alipay.v3.model.*;
import com.jeesite.common.io.FileUtils;

/**
 * 商户转账
 */
public class AlipayFundTransUniApiTransfer {

    AlipayConfig alipayConfig;

    public AlipayFundTransUniApiTransfer() {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com");
        alipayConfig.setAppId("2021004144658211");
        //应用私钥
        alipayConfig.setPrivateKey(FileUtils.readFileToString("csr\\appCertPrivateKey.txt"));
        //应用公钥证书文件路径
        alipayConfig.setAppCertPath("D:\\project\\JAVA\\liuliangka\\web\\src\\main\\resources\\csr\\appCertPublicKey.crt");
        //支付宝公钥证书文件路径
        alipayConfig.setAlipayPublicCertPath("D:\\project\\JAVA\\liuliangka\\web\\src\\main\\resources\\csr\\alipayCertPublicKey_RSA2.crt");
        //支付宝根证书路径
        alipayConfig.setRootCertPath("D:\\project\\JAVA\\liuliangka\\web\\src\\main\\resources\\csr\\alipayRootCert.crt");
        this.alipayConfig = alipayConfig;
    }

    /**
     *
     * @param appId
     * @param privateKeyUrl 应用私钥文件地址
     * @param alipayPublicKeyUrl 支付宝公钥证书文件路径
     * @param rootCertPathUrl  支付宝根证书路径
     * @param appCertPathUrl 应用公钥证书文件路径
     */
    public AlipayFundTransUniApiTransfer(String appId,String privateKeyUrl, String alipayPublicKeyUrl,String rootCertPathUrl,String appCertPathUrl) {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com");
        alipayConfig.setAppId(appId);
        //应用私钥
        alipayConfig.setPrivateKey(FileUtil.readUtf8String(privateKeyUrl));
        //应用公钥证书文件路径
        alipayConfig.setAppCertPath(appCertPathUrl);
        //支付宝公钥证书文件路径
        alipayConfig.setAlipayPublicCertPath(alipayPublicKeyUrl);
        //支付宝根证书路径
        alipayConfig.setRootCertPath(rootCertPathUrl);
        this.alipayConfig = alipayConfig;
    }


    public String transfer(String outBizNoId,String transAmount,String orderTitle,String mobile,String name) throws ApiException {
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        // 初始化alipay参数（全局设置一次）
        defaultClient.setAlipayConfig(alipayConfig);

        // 构造请求参数以调用接口
        AlipayFundTransUniApi api = new AlipayFundTransUniApi();
        AlipayFundTransUniTransferModel data = new AlipayFundTransUniTransferModel();

        // 设置商家侧唯一订单号
        data.setOutBizNo(outBizNoId);

        // 设置订单总金额
        data.setTransAmount(transAmount);

        // 设置描述特定的业务场景
        data.setBizScene("DIRECT_TRANSFER");

        // 设置业务产品码
        data.setProductCode("TRANS_ACCOUNT_NO_PWD");

        // 设置转账业务的标题
        data.setOrderTitle(orderTitle);


        // 设置原支付宝业务单号
//        data.setOriginalOrderId("20190620110075000006640000063056");

        // 设置收款方信息
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentity(mobile);
        payeeInfo.setName(name);
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        data.setPayeeInfo(payeeInfo);

        // 设置业务备注
//        data.setRemark("201905代发");

        // 设置转账业务请求的扩展参数
//        data.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");



        try {
            AlipayFundTransUniTransferResponseModel response = api.transfer(data);
            return response.toJson();
        } catch (ApiException e) {
            AlipayFundTransUniTransferDefaultResponse errorObject = (AlipayFundTransUniTransferDefaultResponse) e.getErrorObject();
            String json = errorObject.toJson();
            System.out.println("调用失败:" + json);
            return json.toString();
        }
    }

    public static void main(String[] args) throws ApiException {
        AlipayFundTransUniApiTransfer transfer = new AlipayFundTransUniApiTransfer();
        String result = transfer.transfer("12345678", "10000", "提现", "18360463221", "李寒冰");
        System.out.println(result);
    }
/*
    public static void main(String[] args) throws ApiException {
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        // 初始化alipay参数（全局设置一次）
        defaultClient.setAlipayConfig(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayFundTransUniApi api = new AlipayFundTransUniApi();
        AlipayFundTransUniTransferModel data = new AlipayFundTransUniTransferModel();

        // 设置商家侧唯一订单号
        data.setOutBizNo("a111b111234");

        // 设置订单总金额
        data.setTransAmount("0.1");

        // 设置描述特定的业务场景
        data.setBizScene("DIRECT_TRANSFER");

        // 设置业务产品码
        data.setProductCode("TRANS_ACCOUNT_NO_PWD");

        // 设置转账业务的标题
        data.setOrderTitle("201905代发");


        // 设置原支付宝业务单号
//        data.setOriginalOrderId("20190620110075000006640000063056");

        // 设置收款方信息
        Participant payeeInfo = new Participant();
//        payeeInfo.setCertType("IDENTITY_CARD");
//        payeeInfo.setCertNo("131121198912060297");
        payeeInfo.setIdentity("18360463221");
        payeeInfo.setName("李寒冰");
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        data.setPayeeInfo(payeeInfo);

        // 设置业务备注
        data.setRemark("201905代发");

        // 设置转账业务请求的扩展参数
        data.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");



        // 设置签名信息
//        SignData signData = new SignData();
//        signData.setOriSign("11");
//        signData.setPartnerId("2088641389541443");
//        signData.setOriAppId("2021004144658211");
//        signData.setOriOutBizNo("a111b1");
//        signData.setOriSignType("RSA2");
//        signData.setOriCharSet("UTF-8");
//        data.setSignData(signData);


        try {
            AlipayFundTransUniTransferResponseModel response = api.transfer(data);
        } catch (ApiException e) {
            AlipayFundTransUniTransferDefaultResponse errorObject = (AlipayFundTransUniTransferDefaultResponse) e.getErrorObject();
            System.out.println("调用失败:" + errorObject);
        }
    }
*/

    private static AlipayConfig getAlipayConfig() {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com");
        alipayConfig.setAppId("2021004144658211");
        //应用私钥
        alipayConfig.setPrivateKey(FileUtils.readFileToString("csr\\appCertPrivateKey.txt"));
        //应用公钥证书文件路径
        alipayConfig.setAppCertPath("D:\\project\\JAVA\\liuliangka\\web\\src\\main\\resources\\csr\\appCertPublicKey.crt");
        //支付宝公钥证书文件路径
        alipayConfig.setAlipayPublicCertPath("D:\\project\\JAVA\\liuliangka\\web\\src\\main\\resources\\csr\\alipayCertPublicKey_RSA2.crt");
        //支付宝根证书路径
        alipayConfig.setRootCertPath("D:\\project\\JAVA\\liuliangka\\web\\src\\main\\resources\\csr\\alipayRootCert.crt");
        return alipayConfig;
    }
}

